package com.wande.dataplatform.controller;

import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.DsSyncConfig;
import com.wande.dataplatform.domain.DsSyncLog;
import com.wande.dataplatform.domain.DsTaskMapping;
import com.wande.dataplatform.domain.dto.DsSyncConfigDTO;
import com.wande.dataplatform.domain.vo.DsTaskMappingVO;
import com.wande.dataplatform.service.IDolphinSchedulerSyncService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * DolphinScheduler同步管理控制器
 *
 * @author DataPlatform
 */
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("/dataplatform/ds-sync")
@Tag(name = "DolphinScheduler同步管理", description = "DolphinScheduler同步管理接口")
public class DolphinSchedulerSyncController extends BaseController {

    private final IDolphinSchedulerSyncService syncService;

    /**
     * 创建同步配置
     */
    @PostMapping("/config")
    @Operation(summary = "创建同步配置")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.INSERT)
    public R<Long> createSyncConfig(@Valid @RequestBody DsSyncConfigDTO dto) {
        return R.ok(syncService.createSyncConfig(dto));
    }

    /**
     * 更新同步配置
     */
    @PutMapping("/config")
    @Operation(summary = "更新同步配置")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.UPDATE)
    public R<Boolean> updateSyncConfig(@Valid @RequestBody DsSyncConfigDTO dto) {
        return R.ok(syncService.updateSyncConfig(dto));
    }

    /**
     * 删除同步配置
     */
    @DeleteMapping("/config/{id}")
    @Operation(summary = "删除同步配置")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.DELETE)
    public R<Boolean> deleteSyncConfig(@PathVariable Long id) {
        return R.ok(syncService.deleteSyncConfig(id));
    }

    /**
     * 查询同步配置列表
     */
    @GetMapping("/config/list")
    @Operation(summary = "查询同步配置列表")
    public TableDataInfo<DsSyncConfig> querySyncConfigList(PageQuery pageQuery) {
        return syncService.querySyncConfigList(pageQuery);
    }

    /**
     * 手动触发同步
     */
    @PostMapping("/trigger/{configId}")
    @Operation(summary = "手动触发同步")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.OTHER)
    public R<Boolean> triggerSync(@PathVariable Long configId) {
        return R.ok(syncService.triggerSync(configId));
    }

    /**
     * 创建ETL任务映射
     */
    @PostMapping("/mapping/etl/{etlTaskId}")
    @Operation(summary = "创建ETL任务映射")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.INSERT)
    public R<Long> createEtlTaskMapping(@PathVariable Long etlTaskId) {
        return R.ok(syncService.createEtlTaskMapping(etlTaskId));
    }

    /**
     * 同步工作流状态
     */
    @PostMapping("/workflow/{workflowCode}/sync")
    @Operation(summary = "同步工作流状态")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.OTHER)
    public R<Boolean> syncWorkflowStatus(@PathVariable Long workflowCode) {
        return R.ok(syncService.syncWorkflowStatus(workflowCode));
    }

    /**
     * 同步实例状态
     */
    @PostMapping("/instance/{instanceId}/sync")
    @Operation(summary = "同步实例状态")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.OTHER)
    public R<Boolean> syncInstanceStatus(@PathVariable Long instanceId) {
        return R.ok(syncService.syncInstanceStatus(instanceId));
    }

    /**
     * 查询任务映射关系
     */
    @GetMapping("/mapping/{platformTaskId}/{platformTaskType}")
    @Operation(summary = "查询任务映射关系")
    public R<DsTaskMapping> queryTaskMapping(
        @PathVariable Long platformTaskId,
        @PathVariable String platformTaskType) {
        return R.ok(syncService.queryTaskMapping(platformTaskId, platformTaskType));
    }

    /**
     * 查询任务映射列表
     */
    @GetMapping("/mapping/list")
    @Operation(summary = "查询任务映射列表")
    public TableDataInfo<DsTaskMappingVO> queryTaskMappingList(PageQuery pageQuery) {
        return syncService.queryTaskMappingList(pageQuery);
    }

    /**
     * 查询同步日志
     */
    @GetMapping("/logs")
    @Operation(summary = "查询同步日志")
    public TableDataInfo<DsSyncLog> querySyncLogs(
        @RequestParam(required = false) Long configId,
        PageQuery pageQuery) {
        return syncService.querySyncLogs(configId, pageQuery);
    }

    /**
     * 构建数据血缘
     */
    @PostMapping("/lineage/{taskId}")
    @Operation(summary = "构建数据血缘")
    @Log(title = "DolphinScheduler同步", businessType = BusinessType.OTHER)
    public R<Boolean> buildDataLineage(@PathVariable Long taskId) {
        return R.ok(syncService.buildDataLineage(taskId));
    }
}
